package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimple;
import bean.MessageForSimpleList;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.adapter.RallAdapter;
import util.dialog.DialogFromMiddle;

/**
 * 围栏列表
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class Rall_A extends BackHandledFragment {

	private View rootView;
	private ListView listView;
	private RallAdapter adapter;
	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_rall_a, null);
		initView();
		initData();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.map_rall);
		rootView.findViewById(R.id.iv_add).setOnClickListener(listener);
		listView = ((ListView) rootView.findViewById(R.id.listview));
	}

	public void initData(){
		ApiUtil.getApiService().getSafeRegions(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimpleList>() {
			@Override
			public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
				try {
					MessageForSimpleList entity = response.body();
					if(entity.getCode().equals("10008")){
						mDatas = entity.getData();
						initAdapter();
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	private void initAdapter(){
		adapter = new RallAdapter(getActivity(),mDatas);
		adapter.setDeleteListener(new RallAdapter.MyItemDelete() {
			@Override
			public void listener(final int item) {
				DialogFromMiddle dialogFormMiddle = new DialogFromMiddle(getActivity());
				dialogFormMiddle.setSureListener(new DialogFromMiddle.Sure() {
					@Override
					public void onSure() {
						// TODO Auto-generated method stub
						ApiUtil.getApiService().deleteSafeRegions(DemoApplication.getToken(),DemoApplication.getDeviceid(),mDatas.get(item).getRegionid()).enqueue(new Callback<MessageForSimple>() {
							@Override
							public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
								try {
									MessageForSimple entity = response.body();
									if(entity.getCode().equals("10008")){
										mDatas.remove(item);
										adapter.notifyDataSetChanged();
									}else{
										Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							@Override
							public void onFailure(Call<MessageForSimple> call, Throwable t) {
								Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
							}
						});
					}
				});
				dialogFormMiddle.show(getString(R.string.dialog_title2),getString(R.string.dialog_content2), "", "",getString(R.string.dialog_yes2),getString(R.string.dialog_no2), true);
			}
		});
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				((SonActivity) getActivity()).addFragment(Rall_B.newInstance(mDatas.get(position).getRegionid(),mDatas.get(position).getCenterlat(),mDatas.get(position).getCenterlng(),mDatas.get(position).getName(),mDatas.get(position).isInalarm()+"",mDatas.get(position).isOutalarm()+"",mDatas.get(position).getRadius()));
			}
		});
	}

	public static Rall_A newInstance() {
		Rall_A fragment = new Rall_A();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_add:
					((SonActivity) getActivity()).addFragment(Rall_B.newInstance("","","","","","",""));
					break;
				default:
					break;
			}
		}
	};

	@Override
	public boolean onBackPressed() {
		return false;
	}

}
